/*
 * File:                OptionsDialog.cs
 * Authors:             Jonathan T. McClain
 * Company:             Sandia National Laboratories
 * Project:             MVC
 *
 * Copyright 2008, Sandia Corporation.  Under the terms of Contract
 * DE-AC04-94AL85000, there is a non-exclusive license for use of this work by
 * or on behalf of the U.S. Government. Export of this program may require a
 * license from the United States Government. See CopyrightHistory.txt for
 * complete details.
 *
 * Reviewers:
 * Review Date:
 * Review Comments:
 *
 * Revision History:
 *
 * $Log: OptionsDialog.cs,v $
 * Revision 1.5  2009/03/11 23:03:43  jtmccl
 * Added a splash screen.  Bug #801 [JTM 20090311]
 * Plugins can now be loaded from the task bar context menu. [JTM 20090311]
 * Added an option to reset the local plugins and restart the application. [JTM 20090310]
 * Added an option to restart the application after changing settings. [JTM 20090310]
 * Fixed an issue where failures during the sync process would cause MVCShell to be unusable.  Bug #1267 [JTM 20090310]
 *
 * Revision 1.4  2008/05/08 20:56:55  jtmccl
 * Added file headers.
 *
 */

using Sandia.MVCShell.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Sandia.MVCShell
{
    /// <summary>
    /// A dialog used to set the application options for MVCShell.
    /// </summary>
    public partial class OptionsDialog : Form
    {
        /// <summary>
        /// Constructor for OptionsDialog.
        /// </summary>
        public OptionsDialog()
        {
            InitializeComponent();

            this.pluginPathTB.Text = Settings.Default.PluginDirectory;
            this.pluginSourceTB.Text = Settings.Default.PluginUpdateDirectory;

            // Disable the plug-in source textbox for non-sync mode.
            if (!Settings.Default.SyncPlugins)
            {
                this.pluginSourceTB.Enabled = false;
                this.sourceBrowseBtn.Enabled = false;
                this.pluginSourceTB.Text = "Plug-in updates are disabled.";
            }
        }

        /// <summary>
        /// Called when the plugin path browse button is clicked.
        /// </summary>
        /// <param name="sender">The button.</param>
        /// <param name="e">The event arguments.</param>
        private void browseBtn_Click(
            object sender, 
            EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            // If the plugin directory exists, make the folder browser
            // dialog start with that directory selected.
            if (Directory.Exists(this.pluginPathTB.Text))
            {
                folderDialog.SelectedPath = this.pluginPathTB.Text;
            }
            // Show the dialog and set the plugin path if the user selects one.
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                this.pluginPathTB.Text = folderDialog.SelectedPath;
            }
        }

        /// <summary>
        /// Called when the plugin source path browse button is clicked.
        /// </summary>
        /// <param name="sender">The button.</param>
        /// <param name="e">The event arguments.</param>
        private void sourceBrowseBtn_Click(
            object sender, 
            EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            // If the plugin source directory exists, make the folder browser
            // dialog start with that directory selected.
            if (Directory.Exists(this.pluginSourceTB.Text))
            {
                folderDialog.SelectedPath = this.pluginSourceTB.Text;
            }
            // Show the dialog and set the plugin source path if the user selects one.
            if (folderDialog.ShowDialog() == DialogResult.OK)
            {
                this.pluginSourceTB.Text = folderDialog.SelectedPath;
            }
        }

        /// <summary>
        /// Called when the OK button is clicked.
        /// </summary>
        /// <param name="sender">The button.</param>
        /// <param name="e">The event arguments.</param>
        private void okBtn_Click(
            object sender, 
            EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Settings.Default.PluginDirectory = this.pluginPathTB.Text;
            // Only save this if we are syncing.
            if (Settings.Default.SyncPlugins)
            {
                Settings.Default.PluginUpdateDirectory = this.pluginSourceTB.Text;
            }
            Settings.Default.Save();
            if (MessageBox.Show(
                "Changes to settings only take effect after application restart. Would you like to restart now?",
                "Application requires restart!",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                Application.Restart();
            }
            else
            {
                this.Close();
            }
        }

        /// <summary>
        /// Called when the cancel button is clicked.
        /// </summary>
        /// <param name="sender">The button.</param>
        /// <param name="e">The event arguments.</param>
        private void cancelBtn_Click(
            object sender, 
            EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}